from google.appengine.api import memcache

from engine.Person import *
from engine.util import *
from guessHomeAddress import *
from guessWorkAddress import *

class Question():
    def __init__(self, text, aux=""):
        self.text = text
        self.aux = aux

def processHomeLocationQuestion(person, answer, address):
    if answer == '1':
        lat, lng = TranslateAddressToCoordinates(address)
        location = Location(lat = lat, lng = lng)
        location.save()
        person.homeLocation = location
        person.save()

def processWorkLocationQuestion(person, answer, address):
    if answer == '1':
        lat, lng = TranslateAddressToCoordinates(address)
        location = Location(lat = lat, lng = lng)
        location.save()
        person.workLocation = location
        person.save()

def processCurrentActivityQuestion(person, answer, address):
    if answer == '1':
        pass

def getHomeLocationQuestion(person):
    r = guessHomeAddress(person)
    if r:
        address, searchTerm = r
        q = Question("Bor du %s?" % address, aux = searchTerm)
        return q

def getWorkLocationQuestion(person):
    r = guessWorkAddress(person)
    if r:
        address, searchTerm = r
        q = Question("Arbejder du hos %s?" % address, aux = searchTerm)
        return q

def getCurrentActivityQuestion(person):
    activity = person.getCalendar().getActivityAtTime(TimeNow())
    if activity:
        q = Question("Er min aktivitet: %s?" % activity.name)
        return q
    

def processAnswer(qid, person, answer, aux=""):
    {
        'currentActivity': processCurrentActivityQuestion,
        'homeLocation': processHomeLocationQuestion,
        'workLocation': processWorkLocationQuestion
    }[qid](person, answer, aux)
    
def getQuestion(qid, person):
    q = {
        'currentActivity': getCurrentActivityQuestion,
        'homeLocation': getHomeLocationQuestion,
        'workLocation': getWorkLocationQuestion
    }[qid](person)
    if q:
        q.id = qid
    return q
